package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.entity.Result;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：seanyang
 * @date ：Created in 2019/6/29
 * @description ：
 * @version: 1.0
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

	@Autowired
	private MemberDao memberDao;

	@Autowired
	private OrderDao orderDao;


	@Override
	public Map<String, Object> getBussinessReport() {
		// 1. 获得今天日期
		// 2. 获得本周周一日期
		// 3. 获得本月第一天日期
		String today = "";
		String weekMonday = "";
		String firstDayInMonth = "";
		try{
			today = DateUtils.parseDate2String(new Date());
			weekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
			firstDayInMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
		}catch(Exception e){
		    e.printStackTrace();
		    throw new RuntimeException("初始化日期失败");
		}
		// 4. 获取与会员有关数据 (今日、本周、本月)
		Integer todayNewMember = memberDao.findMemberCountByDate(today);
		Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(weekMonday);
		Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDayInMonth);
		Integer totalNumber = memberDao.findMemberTotalCount();
		// 5. 获取与预约有关数据
		Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
		Integer thisWeekOrderNumber = orderDao.findOrderCountByAfterDate(weekMonday);
		Integer thisMonthOrderNumber = orderDao.findOrderCountByAfterDate(firstDayInMonth);
		// 6. 获得到诊相关数据
		Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
		Integer thisWeekVisitsNumber = orderDao.findVisitsCountByAfterDate(weekMonday);
		Integer thisMonthVisitsNumber = orderDao.findVisitsCountByAfterDate(firstDayInMonth);
		// 7. 获得套餐热门数据
		List<Map> hotSetmeal = orderDao.findHotSetmeal();
		// 8. 封装数据到map集合
		Map<String,Object> result = new HashMap<>();
		result.put("reportDate",today);
		// 会员相关
		result.put("todayNewMember",todayNewMember);
		result.put("thisWeekNewMember",thisWeekNewMember);
		result.put("thisMonthNewMember",thisMonthNewMember);
		result.put("totalMember",totalNumber);
		// 预约相关
		result.put("todayOrderNumber",todayOrderNumber);
		result.put("thisWeekOrderNumber",thisWeekOrderNumber);
		result.put("thisMonthOrderNumber",thisMonthOrderNumber);
		// 到诊相关
		result.put("todayVisitsNumber",todayVisitsNumber);
		result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
		result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
		// 套餐数据
		result.put("hotSetmeal",hotSetmeal);
		return result;
	}
	/**
	 * 获取性别占比数据
	 */
	@Override
	public Result getGenderPercent() {
		try {
			// 获取性别占比数据
			List<Map<String, String>> genderPercent = memberDao.getGenderPercent();
			// 设置套餐名称集合
			List<String> genders = new ArrayList<>();
			//遍历集合,取每个map对象  {"name":"男性","value":10}
			for (Map<String, String> objectMap : genderPercent) {
				String gender = objectMap.get("name");
				//将数据库中的数字转换成性别
				if ("1".equals(gender)){
					objectMap.put("name","男性");
					gender="男性";
					genders.add(gender);
				}else if ("2".equals(gender)){
					objectMap.put("name","女性");
					gender="女性";
					genders.add(gender);
				}else {
					objectMap.put("name","未填写性别");
					gender="未填写性别";
					genders.add(gender);
				}
			}
			// 构建返回数据
			Map<String, Object> Mapdata = new HashMap<>();
			Mapdata.put("genders", genders);
			Mapdata.put("genderPercent", genderPercent);
			return new Result(true,MessageConst.ACTION_SUCCESS,Mapdata);
		}catch (Exception e){
			return new Result(false,MessageConst.ACTION_FAIL);
		}
	}
	/**
	 * 获取年龄占比数据
	 *{"name":"0-18","value":10},    >=new Date()-18
	 *{"name":"18-30","value":30},   new Date()-18>birthday>=new Date()-30
	 *{"name":"30-45","value":25}	 new Date()-30>birthday>=new Date()-45
	 *{"name":"45以上","value":25}	 new Date()-45>birthday
	 *
	 */
	@Override
	public Result getAgePercent() {
		SimpleDateFormat dff=new SimpleDateFormat("yyyy-MM-dd");
		//获取当前时间
		Calendar c = Calendar.getInstance();
		//0-18岁的出生年份
		c.add(Calendar.YEAR,-18);
		String format1 = dff.format(c.getTime());
		//0-30岁的出生年份
		Calendar d = Calendar.getInstance();
		d.add(Calendar.YEAR,-30);
		String format2 = dff.format(d.getTime());

		//0-45岁的出生年份
		Calendar e = Calendar.getInstance();
		e.add(Calendar.YEAR,-45);
		String format3 = dff.format(e.getTime());
		//结果集
		List<Map<String,Object>> result=new ArrayList<>();
		List<String> ages=new ArrayList<>();
		//时间参数集
		Map<String,String> dateBetween=new HashMap<>();
		try {
			//0-18岁的数据 返回结果{value:百分比}
			dateBetween.put("ldate",format1);
			Map<String, Object> agePercent = memberDao.getAgePercent(dateBetween);
			agePercent.put("name","0-18岁");
			ages.add("0-18岁");
			//添加至结果集
			result.add(agePercent);
			//18-30岁的数据 返回结果{value:百分比}
			dateBetween.clear();
			dateBetween.put("gdate",format1);
			dateBetween.put("ldate",format2);
			Map<String, Object> agePercent2 = memberDao.getAgePercent(dateBetween);
			agePercent2.put("name","18-30岁");
			ages.add("18-30岁");
			//添加至结果集
			result.add(agePercent2);
			//30-45岁的数据 返回结果{value:百分比}
			dateBetween.clear();
			dateBetween.put("gdate",format2);
			dateBetween.put("ldate",format3);
			Map<String, Object> agePercent3 = memberDao.getAgePercent(dateBetween);
			agePercent3.put("name","30-45岁");
			ages.add("30-45岁");
			//添加至结果集
			result.add(agePercent3);
			//大于45岁的数据 返回结果{value:百分比}
			dateBetween.clear();
			dateBetween.put("gdate",format3);
			Map<String, Object> agePercent4 = memberDao.getAgePercent(dateBetween);
			agePercent4.put("name","大于45岁");
			//添加至结果集
			result.add(agePercent4);
			ages.add("大于45岁");

			//手机快速登录用户,没有年龄的占比
			Map<String, Object> agePercent5 = memberDao.getNullAgePercent();
			agePercent5.put("name","未填写年龄");
			//添加至结果集
			result.add(agePercent5);
			ages.add("未填写年龄");
			// 构建返回数据
			Map<String, Object> Mapdata = new HashMap<>();
			Mapdata.put("ages", ages);
			Mapdata.put("agePercent", result);
			return new Result(true, MessageConst.ACTION_SUCCESS,Mapdata);
		} catch (Exception e1) {
			return new Result(false, MessageConst.ACTION_FAIL);
		}
	}

}
